package com.common;

import com.orm.AllService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContext implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    private static SessionFactory sessionFactory;
    private static AllService allService;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContext.applicationContext = applicationContext;
        sessionFactory = (SessionFactory) getBean("sessionFactory");
        allService = (AllService)getBean("allService");
    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 获取一个由Spring管理的Bean
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName) {
        return getApplicationContext().getBean(beanName);
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static Session getOpenedSession()
    {
        return getSessionFactory().openSession();
    }

    public static AllService getAllService() {
        return allService;
    }
}
